//登录 注册 等路由处理
const express = require('express')
const User = require('../modules/user')
const md5  = require('blueimp-md5')
let router = express.Router()

router.get('/',function(req,res){
    res.render('index.html')
})
router.get('/login',function(req,res){
    res.render('login.html')
})
router.post('/login',function(req,res){

})

router.get('/register',function(req,res){
    res.render('register.html')
})
router.post('/register',function(req,res){
    let curUser = new User(req.body)
    curUser.password = md5(md5(curUser.password))
    User.findOne({ email: curUser.email} ,function(err,data){
        if(err){
            return res.status(500).json({
                err_code:500,
                message:'Sever Error!'
            })
        }else {
            if(data) {
                return res.status(200).json({
                    err_code:1,
                    message:'The email adress is registered!'
                })
            }else{
                curUser.save((err)=>{
                    if(err) {
                        return res.status(500).json({
                            err_code:500,
                            message:'Sever Error!'
                        })
                    }else{
                        return res.status(200).json({
                            err_code:0,
                            message:'sucess to register!'
                        })
                    }
                })
            }
        }
    })
    
})

module.exports = router